const Router = require("koa-router");
const pool = require('../database.js');
const {getList,anthVertify,postData,putData,deleteData} =require('../common/middleware.js')
const router = new Router({
  prefix: "/promotion",
});
router.get("/", anthVertify,async(ctx, next) => {
    const statement = `SELECT  promotion.id as id,
    promotion.link AS link, promotion.personId as personId,
		JSON_OBJECT('id', persoldate.id, 'name', persoldate.name,'education',persoldate.education,
      'school',persoldate.school
      ,'phone',persoldate.phone
      ,'email',persoldate.email
      ,'home',persoldate.home
      ,'evaluate',persoldate.evaluate
      ,'experience',persoldate.experience
    ) as persoldate ,c.proskill  AS 'proskill', d.projects  AS 'projects'
    from promotion
		LEFT JOIN persoldate ON persoldate.id=promotion.userId
		
		LEFT JOIN (SELECT promotion.id as id, JSON_ARRAYAGG(JSON_OBJECT('pid', promotion.id,'id', project.id, 'describee', project.describee
    , 'describee', project.describee
    , 'technology', project.technology
    , 'responsibility', project.responsibility
    , 'link', project.link
    , 'ename', project.ename
    )) as projects from promotion LEFT JOIN promotion_pro ON promotion.id=promotion_pro.proId LEFT JOIN project ON promotion_pro.projectId=project. id GROUP BY id) d ON  d.id=promotion.id
   
    LEFT JOIN (SELECT promotion.id as id, JSON_ARRAYAGG(JSON_OBJECT('pid', promotion.id,'id', professionalskills.id, 'professionalSkills', professionalskills.professionalSkills)) as proskill  from promotion  LEFT JOIN promotion_skills ON promotion.id=promotion_skills.proId   LEFT JOIN professionalskills ON promotion_skills.skillId =professionalskills.id GROUP BY id) c ON  c.id=promotion.id`
    await getList(ctx,{statement:statement,otherState:`WHERE promotion.personId=${ctx.user.id}`})
});
router.get("/baseDate", async(ctx, next) => {
  const statement = `SELECT  promotion.id as id,
  promotion.link AS link,
  JSON_OBJECT('id', persoldate.id, 'name', persoldate.name,'education',persoldate.education,
    'school',persoldate.school
    ,'phone',persoldate.phone
    ,'email',persoldate.email
    ,'home',persoldate.home
    ,'evaluate',persoldate.evaluate
    ,'experience',persoldate.experience
  ) as persoldate ,c.proskill  AS 'proskill'
  from promotion
  LEFT JOIN (SELECT promotion.id as id, JSON_ARRAYAGG(JSON_OBJECT('id', professionalskills.id, 'professionalSkills', professionalskills.professionalSkills)) as proskill  from promotion  LEFT JOIN promotion_skills ON promotion.id=promotion_skills.proId   LEFT JOIN professionalskills ON promotion_skills.skillId =professionalskills.id GROUP BY id) c ON  c.id=promotion.id
  LEFT JOIN persoldate ON persoldate.id=promotion.userId WHERE promotion.link=${ctx.query.link} && promotion.id=${ctx.query.eid}`
  await getList(ctx,{statement:statement,notList:true})
});
router.get("/project", async(ctx, next) => {
  const statement = `SELECT 
  JSON_ARRAYAGG(JSON_OBJECT('id', project.id, 'describee', project.describee
  , 'describee', project.describee
  , 'technology', project.technology
  , 'responsibility', project.responsibility
  , 'link', project.link
  , 'ename', project.ename
  , 'problem', project.problem
  )) as projects
  from promotion
  LEFT JOIN promotion_pro ON promotion.id=promotion_pro.proId 
  LEFT JOIN project ON promotion_pro.projectId=project.id
  LEFT JOIN persoldate ON persoldate.id=promotion.userId WHERE promotion.link=${ctx.query.link} && promotion.id=${ctx.query.eid}`
  await getList(ctx,{statement:statement,notList:true})
});
router.get("/link", async(ctx, next) => {
  const statement = `SELECT 
  persoldate.phone AS 'phone',
  persoldate.email AS 'email',
  persoldate.qqLink AS 'qqLink',
  persoldate.wxLink AS 'wxLink'
  from promotion
  LEFT JOIN persoldate ON persoldate.id=promotion.userId  WHERE promotion.link=${ctx.query.link} && promotion.id=${ctx.query.eid}`
  await getList(ctx,{statement:statement,notList:true})
});
router.post("/",anthVertify, async(ctx, next) => {
  try {
    let data=ctx.request.body
   let result2= await getList(ctx,{table:'promotion',otherState:`WHERE link=${data.link} && personId=${ctx.user.id}`,notList:true,notBody:true})
if(result2?.data?.link===data.link){
  const error = new Error('链接名不可重复');
  return  ctx.app.emit('error', error, ctx);
}
let result=   await postData({table:'promotion',data:{link:data.link,userId:data.name,personId:ctx.user.id},ctx:ctx,notNext:true})
await postData({table:'promotion_skills',data:{skillId:data.proskill,proId:result.insertId,userId:ctx.user.id},ctx:ctx,notNext:true,multiInsert:"skillId"})
  await postData({table:'promotion_pro',data:{projectId:data.projects,proId:result.insertId,userId:ctx.user.id},ctx:ctx,multiInsert:"projectId"})
  } catch (error) {
    console.log(error);
  }
});
router.put("/", async(ctx, next) => {
  });
  router.delete("/:id", async(ctx, next) => {
    let id=ctx.params.id
   await deleteData('promotion',id,ctx)
  });
module.exports = router;
